package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.util.DataUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.saas.domain.dao.CreditLimitDao;
import com.xbongbong.saas.domain.entity.ext.CreditLimitEntityExt;
import com.xbongbong.saas.model.CreditLimitModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author xingxing.xiao
 */
@Service("creditLimitModel")
public class CreditLimitModelImpl implements CreditLimitModel {

    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private CreditLimitDao creditLimitDao;

    @Override
    public Integer insert(CreditLimitEntityExt entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Integer success = creditLimitDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CREDIT_LIMIT);
        paasEsModel.insert(insertDTO);
        return success;
    }

    @Override
    public void insertBatch(List<CreditLimitEntityExt> list, boolean isImport) throws XbbException {
        creditLimitDao.insertBatch(list);
        //ES数据插入
        List<InsertDTO> insertList = new ArrayList<>();
        for (PaasFormDataEntity item : list) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(item.getCorpid() + "_" + item.getId());
            item.setDataId(item.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(item, true, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CREDIT_LIMIT);
            insertList.add(insertDTO);
        }
        if (isImport) {
            paasEsModel.insertBatch(insertList, WriteRequest.RefreshPolicy.NONE);
        } else {
            paasEsModel.insertBatch(insertList);
        }
    }

    @Override
    public List<CreditLimitEntityExt> findEntities(Map<String, Object> param) {
        return creditLimitDao.findEntities(param);
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return creditLimitDao.getEntitiesCount(param);
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> list, String corpid) throws XbbException {
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(list)) {
            integer = creditLimitDao.updateBatch(list, corpid);
            paasEsModel.updateBatchMuchField(list, IndexTypeEnum.IDX_SAAS_CREDIT_LIMIT);
        }
        return integer;
    }

    @Override
    public Integer update(CreditLimitEntityExt entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        Integer success = creditLimitDao.update(entity);
        if (Objects.equals(1, success)) {
            // 更新时间
            creditLimitDao.updateTime(DateTimeUtil.getInt(), entity.getId(), entity.getCorpid());
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entity, paasFormDataEntity);
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CREDIT_LIMIT);
            paasEsModel.update(insertDTO);
        }
        return success;
    }

    @Override
    public CreditLimitEntityExt getByKey(Long key, String corpid) {
        return creditLimitDao.getByKey(key, corpid);
    }

    @Override
    public CreditLimitEntityExt getByCustomerId(String customerId, String corpid) {
        return creditLimitDao.getByCustomerId(customerId, corpid);
    }
}
